/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;
import com.opensymphony.xwork2.*;
import bookstore.iservice.*;
import bookstore.service.*;
import bookstore.dao.*;
import bookstore.model.*;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.LOGIN;
import static com.opensymphony.xwork2.Action.SUCCESS;
import static java.lang.System.out;
import java.util.List;
import java.util.Map;
/**
 *
 * @author cjx
 */
public class OrderAction extends ActionSupport
{
    private float total;
    private String reciever;
    private String phone;
    private String address;
    private String email;
    private String zipcode;

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }

    public String getReciever() {
        return reciever;
    }

    public void setReciever(String reciever) {
        this.reciever = reciever;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getPayWay() {
        return payWay;
    }

    public void setPayWay(String payWay) {
        this.payWay = payWay;
    }

    public String getSendWay() {
        return sendWay;
    }

    public void setSendWay(String sendWay) {
        this.sendWay = sendWay;
    }
    private String payWay;
    private String sendWay;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
    private IOrderService orderService = new OrderService();
    private Order order = null;
    private IOrderDetailsService orderDetailsService = new OrderDetailsService();
    private Orderdetails orderdetail = null;
   public String execute()
   {
       
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        Customer customer =(Customer) session.get("bsCustomer");
        List<Good> goodList = (List<Good>) session.get("goodList");
        int ord_id = 0;     
        order = new Order();
        order.setCustomer(customer);
        order.setOrderState("asdasd");
        order.setPayWay("qwqq");
        order.setSendWay("asdasd");
        order.setTotal(total);
        orderService.setOrderDao(new OrderDao());
        ord_id=orderService.addOrder(order);
        order.setId(ord_id);
         for(int i = 0;i<goodList.size();++i)
        {
            orderdetail = new Orderdetails();
            total += goodList.get(i).getTotal();
            orderdetail.setOrder(order);
            orderdetail.setBook(goodList.get(i).getBook());
            orderdetail.setCount(goodList.get(i).getCount());
            orderdetail.setTotal(goodList.get(i).getTotal());
            orderDetailsService.setDetailDao(new OrderDetailsDao());
            orderDetailsService.addOrderDetails(orderdetail);
        }      
        session.remove("goodList");
      return SUCCESS;
   }
   public String show(){
       ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        Customer customer =(Customer) session.get("bsCustomer");
       orderService.setOrderDao(new OrderDao());
       List<Order> orderList=orderService.findOrder(customer);
       session.put("orderList", orderList);
        return SUCCESS;
   }
    
}
